<?php
//set_time_limit(0) ;
/** 
 * 数据导出 
 * @param array $title   标题行名称 
 * @param array $data   导出数据 
 * @param string $fileName 文件名 
 * @param string $savePath 保存路径 
 * @param $type   是否下载  false--保存   true--下载 
 * @param $isDown 网页下载  
 * @return string   返回文件全路径 
 * @throws PHPExcel_Exception 
 * @throws PHPExcel_Reader_Exception 
 */
 
/* 
$title = array('姓名','部门','迟到','早退','请假','调休','外勤','缺勤','加班','未打卡');
$data  = array(
			array('张三','研发部',1,0,0,0,0,0,0,0,),
			array('李四','研发部',1,0,0,0,0,0,0,0,)
		);
$fileName = '考勤统计表';
$isDown   = false;*/

$postdata = json_decode(file_get_contents('php://input'),true);
$title 		= json_decode(unserialize($postdata['title']),true);
$data  		= json_decode(unserialize($postdata['data']),true);
$fileName  	= $postdata['fileName'];
$isDown  	= $postdata['isDown'];


$savePath = './../../images/export/';

function exportExcel($title=array(), $data=array(), $fileName='', $savePath='./', $isDown=false){  
    require_once 'PHPExcel.php';
    $obj = new PHPExcel();  
  
    //横向单元格标识  
    $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K');
      
    $obj->getActiveSheet(0)->setTitle('sheet名称');   //设置sheet名称  
    $_row = 0;   //设置纵向单元格标识  
    if($title){  
        $_cnt = count($title);  
        //$obj->getActiveSheet(0)->mergeCells('A'.$_row.':'.$cellName[$_cnt-1].$_row);   //合并单元格  
        //$obj->setActiveSheetIndex(0)->setCellValue('A'.$_row, '数据导出：'.date('Y-m-d H:i:s'));  //设置合并后的单元格内容  
        $_row++;  
        $i = 0;  
        foreach($title AS $v){   
			//设置表头
            $obj->setActiveSheetIndex(0)->setCellValue($cellName[$i].$_row, $v);  
            $i++;  
        }  
        $_row++;  
    }  
  
    //填写数据
    /*if($data){  
        $i = 0;
		foreach($data AS $_v){  
            $j = 0;  
            foreach($_v AS $_cell){  
                $obj->getActiveSheet(0)->setCellValue($cellName[$j] . ($i+$_row), $_cell);  
                $j++;  
            }  
            $i++;  
        }
    }*/
	//填写数据
	$key_name = array('user_name','bumen','chidao','zaotui','qingjia','tiaoxiu','waiqin','overtime','duty','clock');
	if($data){  
       foreach($data as $k=>$v){			
			for($i=0;$i<10;$i++){				
				$obj->getActiveSheet(0)->setCellValue($cellName[$i] . ($k+$_row), $v[$key_name[$i]]);				
			}
	   }
    }
      
    //文件名处理  
    if(!$fileName){  
        $fileName = uniqid(time(),true);  
    }  
  
    $objWrite = PHPExcel_IOFactory::createWriter($obj, 'Excel2007');  
  
    if($isDown){   //网页下载  
        header('pragma:public');  
        header("Content-Disposition:attachment;filename=$fileName.xls");  
        $objWrite->save('php://output');exit;  
    }  
  
    $_fileName = iconv("utf-8", "gb2312", $fileName);   //转码  
    $_savePath = $savePath.$_fileName.'.xlsx';  
    $objWrite->save($_savePath);  
	
	$url = 'http://'.$_SERVER['HTTP_HOST'].substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],'/')+1).'index.php/Admin/qr_code_callback';
	
	//当前目录
	$dir = "plugins/PHPExcel/";
	//文件地址 
	$dres = "http://".$_SERVER['HTTP_HOST'].substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],'/')+1);
	$path = str_replace($dir,"",$dres);
	
	return $path.'images/export/'.$fileName.'.xlsx';
    //return $savePath.$fileName.'.xlsx';  
}  

$res = exportExcel($title, $data, $fileName, $savePath, $isDown);
$res = $res?$res:'';
echo json_encode($res);
exit;






